﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ece750project
{
    public struct TimeSlots_type
    {
        public int P_ID;
        public int S_ID;
        public int D_ID;
        public int Sechedual_ID;
        public int status;
        public int Length;
        public DateTime SDate;
        public DateTime STime;
 
    }

    public struct doc_srvice_type
    {
        public int ID;
        public bool is_service;
    }

    public struct docs_for_srvice_type
    {
        public int Doc_ID;
        public int service_ID;
    }

    public partial class Appointments : Form
    {
        public Appointments()
        {
            InitializeComponent();
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node == null) return;
            if (Convert.ToString(e.Node.Tag) == "0") return;
            doc_srvice_type DS_Tag = (doc_srvice_type)e.Node.Tag;

            if (DS_Tag.is_service == true)
            {
                Fill_docs_for_service(DS_Tag.ID);
            }

        }

        private void Appointments_Load(object sender, EventArgs e)
        {

            if (abc_app.Appointments_para.Direct_appointment_mode == true)
            {
                lblpatient_name.Text = DB_BusinessRules.get_person_name(abc_app.Appointments_para.Patient_id);
                Fill_docs_service_timeslots(abc_app.Appointments_para.Patient_id);
            }
            else
                if (abc_app.Appointments_para.Patient_id != -1)
                {
                    lblpatient_name.Text = DB_BusinessRules.get_person_name(abc_app.Appointments_para.Patient_id);
                }
                else
                {
                    lblpatient_name.Text = "There is no Patient selected";
                }
                Fill_Dept_Services();
        }
        public void Fill_docs_for_service(int service_id)
        {
            listDoctors.Items.Clear();
            DataTable DT = DB_BusinessRules.get_docs_for_service(service_id);
            if (DT == null) return;
            for (int i = 0; i <= DT.Rows.Count - 1; i++)
            {
                string Fname, Lname;
                int doc_id;

                doc_id = Convert.ToInt32(DT.Rows[i]["person_id"]);
                Fname = DT.Rows[i]["first_name"].ToString();
                Lname = DT.Rows[i]["last_name"].ToString();
                ListViewItem IT = listDoctors.Items.Add(Lname + ", " + Fname);
                docs_for_srvice_type DFS_Tag = new docs_for_srvice_type();
                DFS_Tag.Doc_ID = doc_id;
                DFS_Tag.service_ID = service_id;
                IT.Tag = DFS_Tag;
            }
            GC.Collect();
        }

        public void Fill_Dept_Services()
        {
            departmet_viewtree.Nodes.Clear();
            DataTable DT = DB_BusinessRules.get_medical_dep_list();
            TreeNode RootN = departmet_viewtree.Nodes.Add("Medical Departments");
            RootN.Tag = "0";
            if (DT == null) return;
            for (int i = 0; i <= DT.Rows.Count - 1; i++)
            {
                string dept_name;
                int did;

                did = Convert.ToInt32(DT.Rows[i]["dept_id"]);
                dept_name = DT.Rows[i]["dept_name"].ToString();

                TreeNode TNode = RootN.Nodes.Add("d" + did, dept_name);
                doc_srvice_type DS_Tag = new doc_srvice_type();
                DS_Tag.ID = did;
                DS_Tag.is_service = false;
                TNode.Tag = DS_Tag;

                DataTable DT2 = DB_BusinessRules.get_dep__services_list(did);
                for (int ii = 0; ii <= DT2.Rows.Count - 1; ii++)
                {
                    int Sid;
                    string title;
                    Sid = Convert.ToInt16(DT2.Rows[ii]["service_id"].ToString());
                    title = Convert.ToString(DT2.Rows[ii]["service_name"]);

                    TreeNode TTNode = TNode.Nodes.Add("s" + Sid, title);
                    doc_srvice_type DSS_Tag = new doc_srvice_type();
                    DSS_Tag.ID = Sid;
                    DSS_Tag.is_service = true;
                    TTNode.Tag = DSS_Tag;
                }

            }
            GC.Collect();
        }

        private void listDoctors_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        public void Fill_docs_service_timeslots(int service_id, int doc_id)
        {
            doctor_time_slots.Items.Clear();
            DataTable DT = DB_BusinessRules.get_doc_service_schedule(service_id, doc_id);
            if (DT == null) return;
            for (int i = 0; i <= DT.Rows.Count - 1; i++)
            {
                string PatientName = "",Staff_name="" ,txtStatus = "";
                TimeSlots_type TS_Tag = new TimeSlots_type();
                TS_Tag.D_ID = doc_id;
                TS_Tag.S_ID = service_id;
                TS_Tag.Sechedual_ID = Convert.ToInt32(DT.Rows[i]["schedule_id"]);
                TS_Tag.P_ID = DT.Rows[i]["patient_id"] == DBNull.Value ? -1 : Convert.ToInt32(DT.Rows[i]["patient_id"]);

                TS_Tag.status = Convert.ToInt16(DT.Rows[i]["status"]);
                TS_Tag.SDate = Convert.ToDateTime(DT.Rows[i]["service_date"]);
                TS_Tag.STime = Convert.ToDateTime(DT.Rows[i]["service_time"]);
                TS_Tag.Length = Convert.ToInt16(DT.Rows[i]["length"]);

                PatientName = DT.Rows[i]["patient_name"].ToString();
                Staff_name = DT.Rows[i]["staff_name"].ToString();
                if (TS_Tag.status == 1) txtStatus = "Available";
                if (TS_Tag.status == 2) txtStatus = "Boocked";
                if (TS_Tag.status == 3) txtStatus = "Canceld";
                if (TS_Tag.status == 4) txtStatus = "Confirmed";


                ListViewItem IT = doctor_time_slots.Items.Add(TS_Tag.SDate.ToLongDateString());
                IT.SubItems.Add(TS_Tag.STime.ToShortTimeString());
                IT.SubItems.Add(TS_Tag.Length.ToString() + " Minutes");
                IT.SubItems.Add(txtStatus);
                IT.SubItems.Add(PatientName);
                IT.SubItems.Add(Staff_name);
                IT.Tag = TS_Tag;
                //IT.ToolTipText = "With Doctor: " + DB_BusinessRules.get_person_name(TS_Tag.D_ID);

                if (TS_Tag.status == 1) IT.ForeColor = Color.Green;
                if (TS_Tag.status == 2) IT.ForeColor = Color.Red;
                if (TS_Tag.status == 3) IT.ForeColor = Color.GreenYellow;
                if (TS_Tag.status == 4) IT.ForeColor = Color.Blue;
            }
            GC.Collect();
        }

        public void Fill_docs_service_timeslots(int patient_ID)
        {
            doctor_time_slots.Items.Clear();
            DataTable DT = DB_BusinessRules.get_patient_schedule(patient_ID);
            if (DT == null) return;
            for (int i = 0; i <= DT.Rows.Count - 1; i++)
            {
                string PatientName = "", Staff_name = "", txtStatus = "";
                TimeSlots_type TS_Tag = new TimeSlots_type();
                TS_Tag.D_ID = Convert.ToInt32(DT.Rows[i]["doctor_id"]);
                TS_Tag.S_ID = Convert.ToInt32(DT.Rows[i]["service_id"]);
                TS_Tag.Sechedual_ID = Convert.ToInt32(DT.Rows[i]["schedule_id"]);
                TS_Tag.P_ID = DT.Rows[i]["patient_id"] == DBNull.Value ? -1 : Convert.ToInt32(DT.Rows[i]["patient_id"]);

                TS_Tag.status = Convert.ToInt16(DT.Rows[i]["status"]);
                TS_Tag.SDate = Convert.ToDateTime(DT.Rows[i]["service_date"]);
                TS_Tag.STime = Convert.ToDateTime(DT.Rows[i]["service_time"]);
                TS_Tag.Length = Convert.ToInt16(DT.Rows[i]["length"]);

                PatientName = DT.Rows[i]["patient_name"].ToString();
                Staff_name = DT.Rows[i]["staff_name"].ToString();
                if (TS_Tag.status == 1) txtStatus = "Available";
                if (TS_Tag.status == 2) txtStatus = "Boocked";
                if (TS_Tag.status == 3) txtStatus = "Canceld";
                if (TS_Tag.status == 4) txtStatus = "Confirmed";


                ListViewItem IT = doctor_time_slots.Items.Add(TS_Tag.SDate.ToLongDateString());
                IT.SubItems.Add(TS_Tag.STime.ToShortTimeString());
                IT.SubItems.Add(TS_Tag.Length.ToString() + " Minutes");
                IT.SubItems.Add(txtStatus);
                IT.SubItems.Add(PatientName);
                IT.SubItems.Add(Staff_name);
                IT.Tag = TS_Tag;
                IT.ToolTipText = "With Doctor: " + DB_BusinessRules.get_person_name(TS_Tag.D_ID);

                if (TS_Tag.status == 1) IT.ForeColor = Color.Green;
                if (TS_Tag.status == 2) IT.ForeColor = Color.Red;
                if (TS_Tag.status == 3) IT.ForeColor = Color.GreenYellow;
                if (TS_Tag.status == 4) IT.ForeColor = Color.Blue;
            }
            GC.Collect();
        }


        private void listDoctors_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            docs_for_srvice_type TAG = (docs_for_srvice_type)e.Item.Tag;
            Fill_docs_service_timeslots(TAG.service_ID, TAG.Doc_ID);

        }

        private void doctor_time_slots_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Right) return;
            try
            {
                if (doctor_time_slots.SelectedItems == null) return;
                TimeSlots_type TS_Tag = (TimeSlots_type)doctor_time_slots.SelectedItems[0].Tag;

                MMenu_Book.Enabled = false;
                MMenu_Cancel.Enabled = false;
                MMenu_Confirm.Enabled = false;

                if (TS_Tag.status == 1)//Available
                {
                    if (abc_app.Appointments_para.Patient_id != -1)
                    {
                        MMenu_Book.Enabled = true;
                    }
                    
                    Menu_Timeslots.Show(doctor_time_slots, e.Location);
                }
                else if (TS_Tag.status == 2)//Booked
                {
                    if (abc_app.Appointments_para.Direct_appointment_mode == true)
                    {
                        if (TS_Tag.P_ID == abc_app.Appointments_para.Patient_id)
                        {
                            MMenu_Cancel.Enabled = true;
                            MMenu_Confirm.Enabled = true;
                        }
                    }
                    Menu_Timeslots.Show(doctor_time_slots, e.Location);
                }

                else if (TS_Tag.status == 3)//Canceled
                {
                    if (abc_app.Appointments_para.Patient_id!=-1)
                    {

                        MMenu_Book.Enabled = true;
                    }
                    Menu_Timeslots.Show(doctor_time_slots, e.Location);
                }
            }
            catch (Exception ex)
            {
                return;
            }

       }

        private void MMenu_Book_Click(object sender, EventArgs e)
        {
            TimeSlots_type TS_Tag = (TimeSlots_type)doctor_time_slots.SelectedItems[0].Tag;
            DB_BusinessRules.book_appointment(TS_Tag.Sechedual_ID, abc_app.Appointments_para.Patient_id,abc_app.Appointments_para.Staff_id);
        }

        private void MMenu_Cancel_Click(object sender, EventArgs e)
        {
            TimeSlots_type TS_Tag = (TimeSlots_type)doctor_time_slots.SelectedItems[0].Tag;
            DB_BusinessRules.cancel_appointment(abc_app.Appointments_para.Staff_id, TS_Tag.Sechedual_ID);
			Fill_docs_service_timeslots(abc_app.Appointments_para.Patient_id);

        }

        private void MMenu_Confirm_Click(object sender, EventArgs e)
        {
            TimeSlots_type TS_Tag = (TimeSlots_type)doctor_time_slots.SelectedItems[0].Tag;
            if (TS_Tag.SDate.ToShortDateString() == DateTime.Today.ToShortDateString())
            {
                DB_BusinessRules.Confirm_appointment(abc_app.Appointments_para.Staff_id, TS_Tag.Sechedual_ID);
				Fill_docs_service_timeslots(abc_app.Appointments_para.Patient_id);
            }
            else
            {
                abc_app.msgbox("You can not confirm an appointment before 24 houres of the visit time", "Confirmation Cancled", Microsoft.VisualBasic.MsgBoxStyle.Exclamation);
            }

        }

        private void doctor_time_slots_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}
